package com.jash.sharetest;

import android.os.Handler;
import android.os.Message;
import android.support.v4.text.TextUtilsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class MainActivity extends AppCompatActivity implements PlatformActionListener, Handler.Callback {

    private static final String TAG = MainActivity.class.getSimpleName();
    private TextView uName;
    private SimpleDraweeView uIcon;
    private EditText pNumber;
    private EditText ver;
    private Handler handler = new Handler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        SMSSDK.initSDK(this, "17c9af4dc368b", "daaa9d10b1a39303d54c0cfa1467d2c2");
        SMSSDK.registerEventHandler(new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    handler.obtainMessage(0, event, result, data).sendToTarget();
//                    if (result == SMSSDK.RESULT_COMPLETE) {
//                        Toast.makeText(MainActivity.this, "验证通过", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
//                    }
                }
//                if (result == SMSSDK.RESULT_COMPLETE) {
//                    //回调完成
//                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//                        //提交验证码成功
//
//                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//                        //获取验证码成功
//                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
//                        //返回支持发送验证码的国家列表
//                    }
//                }else{
//                    ((Throwable)data).printStackTrace();
//                }
            }
        });
        setContentView(R.layout.activity_main);
        uName = (TextView) findViewById(R.id.user_name);
        uIcon = (SimpleDraweeView) findViewById(R.id.user_icon);
        ShareSDK.initSDK(this);
        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        PlatformDb db = weibo.getDb();
        String token = db.getToken();
        String userName = db.getUserName();
        String userIcon = db.getUserIcon();
        if (!TextUtils.isEmpty(token)) {
            uName.setText(userName);
            uIcon.setImageURI(userIcon);
        }
        pNumber = (EditText) findViewById(R.id.phone_number);
        ver = (EditText) findViewById(R.id.verification);
    }
    public void login(View view) {
        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        PlatformDb db = weibo.getDb();
        String token = db.getToken();
        String userName = db.getUserName();
        String userIcon = db.getUserIcon();
        if (!TextUtils.isEmpty(token)) {
            weibo.removeAccount(true);
        } else {
            weibo.setPlatformActionListener(this);
            //authorize与showUser单独调用一个即可
//        weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
            weibo.showUser(null);//授权并获取用户信息
            //移除授权
            //weibo.removeAccount(true);
        }
    }
    public void showShare(View view) {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("分享");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        String name = platform.getDb().getUserName();
        platform.getDb().getUserIcon();
        uName.setText(name);
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            Log.d(TAG, "onComplete: " + entry.getKey() + " : " + entry.getValue());
        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Toast.makeText(this, "onError", Toast.LENGTH_SHORT).show();
        throwable.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Toast.makeText(this, "onCancel", Toast.LENGTH_SHORT).show();
    }
    public void sendSMS(View view) {
        String s = pNumber.getText().toString();
        if (!TextUtils.isEmpty(s)) {
            SMSSDK.getVerificationCode("+86", s);
        }
    }
    public void verification(View view){
        String s = pNumber.getText().toString();
        String v = ver.getText().toString();
        if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(v)) {
            SMSSDK.submitVerificationCode("+86", s, v);
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        int result = msg.arg2;
        if (result == SMSSDK.RESULT_COMPLETE) {
            Toast.makeText(MainActivity.this, "验证通过", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}
